/**
 * @file	proprietary_rf.c
 * @author	chipsea
 * @brief	
 * @version	0.1
 * @date	2021-01-08
 * @copyright Copyright (c) 2020, CHIPSEA Co., Ltd.
 * @note
 */
 
#ifndef __PROPRIETARY_RF_H
#define __PROPRIETARY_RF_H

/*********************************************************************
 * MACROS
 */
#define RF_SET_SYNCWORD(x)                 		PHY_REG_WT(0x4003004c,(x))
#define RF_SET_CRC_SEED(x)                 		subWriteReg(0x40030048,23,0,(x))
#define RF_SET_WHITEN_SEED(x)              		subWriteReg(0x40030048,31,24,(x))

#define RF_HW_SCAN_DELAY                   		(80)
#define RF_HW_BB_DELAY                     		(90)
#define RF_HW_AFE_DELAY                    		( 8)
#define RF_HW_PLL_DELAY                    		(60)

#define RF_PKT_FMT_1M                      		(0x01)
#define RF_PKT_FMT_2M                      		(0x02)
#define RF_PKT_FMT_500K                    		(0x05)
#define RF_PKT_FMT_125K                    		(0x06)

#define RF_PKT_PAYLOAD_SIZE						247

typedef enum
{
	RF_EVT_TX_DONE		= 0,	//transmit OK
	RF_EVT_RX_DONE		= 1,	//receive OK
	RF_EVT_RX_TIMEOUT 	= 2,	//receive timeout
	RF_EVT_RX_PKT_ERR	= 3,	//receive CRC error
	RF_EVT_TRX_DONE		= 4,	//first transmit and then receive mode: receive OK
	RF_EVT_TRX_TIMEOUT	= 5,	//first transmit and then receive mode: receive timeout
	RF_EVT_TRX_PKT_ERR	= 6,	//first transmit and then receive mode: receive CRC error
    RF_EVT_TRX_WAIT     = 7,	//wait response over
    RF_EVT_START_RX     = 8,
}rfEvtType_t;

typedef enum
{
	RF_PHY_ST_TX_ONLY	= 0x00,		//only transmit
	RF_PHY_ST_RX_ONLY 	= 0x01,		//only receive
	RF_PHY_ST_TRX_ONLY 	= 0x02,		//after transmit, change into receive. it's often used in auto-acknowledge
	RF_PHY_ST_IDLE		= 0xFF		//idle 
}rfPhyStatus_t;
extern rfPhyStatus_t s_phyStatus;

typedef struct
{
	uint8 rfEvent;		//reference to rfEvtType_t
	
	uint16 rfFoff;		//rf frequency offset
	uint8 rfRssi;		//rf RSSI
	uint8 rfCarrSens;
	
	uint8 header;		//receive packet header
	uint8 length;		//receive packet length
	uint8 *pPayload;	//receive packet
} rfEvent_t;

typedef uint8_t (*rf_Hdl_t)(rfEvent_t *evt);
typedef void (*user_rf_rx_start_t)(void);
typedef void (*user_rf_irq_process_t)(uint8_t tr_mode, uint32_t irq_status);

typedef struct rfPktCfg 
{
    uint8_t     pktFmt;		//RF_PKT_FMT_1M, RF_PKT_FMT_2M, RF_PKT_FMT_500K or RF_PKT_FMT_100K
    uint8_t     pduLen;		//PDU length, when use CRC, this parameter is ignored
    uint8_t     wtSeed;		//whiten seed
    uint8_t     crcFmt;		//LL_HW_CRC_BLE_FMT or LL_HW_CRC_NULL
    uint32_t    crcSeed;	//CRC seed
    uint32_t    syncWord;	//sync word
}rfPktCfg_t;

typedef struct rfPhyCfg
{
    uint8_t 	bleEnable;	//enable ble
	uint8_t 	autoAck;	//auto send acknowledge packet when this value is not zero
    uint8_t     rfChn;		//rf channel. range is 0~83. rf frequency is 2400~2483MHz.(2400+rfChn)
	uint16_t    rxOnlyTO;	//receive timeout, only used in RF_PHY_MODE_RX_ONLY. time unit is 1us
    uint16_t    rxAckTO;	//receive ACK timeout, only used in RF_PHY_MODE_TRX_ONLY. time unit is 1us
}rfPhyCfg_t;
extern rfPhyCfg_t s_phyCfg;

extern __align(4) uint8_t rfBufRx[256];
extern rfPktCfg_t  s_pktCfg;
extern rf_Hdl_t rf_handler;

uint8_t rf_hw_read_rfifo(uint8_t* rxPkt, uint16_t* pktLen, uint32_t* pktFoot0, uint32_t* pktFoot1);
extern void rf_register_for_irq_callback(rf_Hdl_t cb);
extern void user_rf_rx_start_fun_set(user_rf_rx_start_t fun);
extern void user_rf_irq_process_cb(user_rf_irq_process_t cb);
void rf_hw_pktFmt_config(rfPktCfg_t *cfg);
void rf_hw_phy_config(rfPhyCfg_t *cfg);
void rf_hw_channel_set(uint8 channel);
void rf_tx_start(void);
void rf_rx_start(void);
void rf_tx_ack_start(void);
void rf_stop(void);
void rf_tx_buf_update(uint8_t header, uint8_t length,  uint8_t* payload);
void rf_tx_ack_update(uint8_t header, uint8_t length,  uint8_t* payload);
uint32_t rf_rx_time_get(void);
rfPhyStatus_t rf_phy_status_get(void);
void RF_IRQHandler(void);
void rf_irq_process(uint8_t tr_mode, uint32_t irq_status);

#endif 

